package com.lvmama.rhino.web;

import com.alibaba.fastjson.JSONArray;
import com.lvmama.rhino.enums.CommonParentType;
import com.lvmama.rhino.pojo.OrderInfo;
import com.lvmama.rhino.pojo.VisitorInfo;
import com.lvmama.rhino.service.OrderService;
import com.lvmama.rhino.service.StatisticCodeService;
import com.lvmama.rhino.service.VisitorService;
import com.lvmama.rhino.utils.DateUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: VisitorsController
 * @Description: TODO
 * @author shenlinli
 * @date 2017年5月23日 下午1:48:21
 */
@Controller
@RequestMapping("visitors")
public class VisitorsController {

	@Autowired
	private VisitorService visitorService;

	@Autowired
	private StatisticCodeService statisticCodeService;

	@Autowired
	private OrderService orderService;

	@RequestMapping("visitorsAnalysis")
	public String visitorsAnalysis(ModelMap model) {
		return "application/visitorsAnalysis";
	}

	/**
	 * @Title: visitorChartData
	 * @Description: 访客图表数据
	 * @param @param chooseTime
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("visitorChartData")
	@ResponseBody
	public Map<String, Object> visitorChartData(String chooseTime, String type) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		VisitorInfo visitor = new VisitorInfo();
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(chooseTime);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");

		visitor.setStartDate(beginDate);
		visitor.setEndDate(endDate);
		List<VisitorInfo> visitorListByDate = new ArrayList<VisitorInfo>();
		List<VisitorInfo> visitorListByPlatform = new ArrayList<VisitorInfo>();
		if ("new_visitor".equals(type)) {
			visitorListByDate = visitorService.getVisitorListByDate(visitor);
			visitorListByPlatform = visitorService
					.getVisitorListByPlatform(visitor);
		} else {
			visitorListByDate = visitorService.getRegisterListByDate(visitor);
			visitorListByPlatform = visitorService
					.getRegisterListByPlatform(visitor);
		}

		int amount = 0;
		// 柱状图数据
		JSONArray arr = new JSONArray();
		List<String> platformList = new ArrayList<>();
		for (VisitorInfo platform : visitorListByPlatform) {
			platformList.add(platform.getPlatformCode());
			amount += platform.getAmount();
			JSONObject o = new JSONObject();
			o.put("platform",platform.getPlatformCode());
			o.put("amount",platform.getAmount());
			arr.add(o);
		}
		// 折线图数据
		String[] dateAxis = DateUtil.getBetweenTime(beginDate, endDate,
				"yyyy/MM/dd");
		List<String> dateList = Arrays.asList(dateAxis);

		JSONArray arr2 = new JSONArray();
		for(String date:dateList){
			for (String platform : platformList) {
				Integer amountTomal = 0;
				for (VisitorInfo visitorinfo : visitorListByDate) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
					String time = sdf.format(visitorinfo.getOperDate());
					if (platform.equals(visitorinfo.getPlatformCode()) && date.equals(time)) {
						amountTomal += visitorinfo.getAmount();
					}
				}
				JSONObject o = new JSONObject();
				o.put("platform",platform);
				o.put("time",date);
				o.put("amount",amountTomal);
				arr2.add(o);
			}
		}
		resultMap.put("platformList", platformList);
		resultMap.put("amount", amount);
		resultMap.put("jsonData", arr);
		resultMap.put("jsonData2", arr2);
		return resultMap;
	}

	/**
	 * @Title: visitorPlatformData
	 * @Description: 平台各渠道详细数据
	 * @param @param chooseTime
	 * @param @param type
	 * @param @param platform
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("visitorPlatformData")
	@ResponseBody
	public Map<String, Object> visitorPlatformData(String chooseTime,
			String type, String platform) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		VisitorInfo visitor = new VisitorInfo();
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(chooseTime);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");
		visitor.setStartDate(beginDate);
		visitor.setEndDate(endDate);

		List<VisitorInfo> visitorListByPlatform = new ArrayList<VisitorInfo>();
		if ("new_visitor".equals(type)) {
			if ("wap".equals(platform)) {
				platform = "2g53";
			}else if("pc".equals(platform)) {
				platform = "2qw6";
			}
			visitor.setPlatformCode(platform);
			visitorListByPlatform = visitorService
					.getVisitorListByChannel(visitor);
		} else {
			List<String> statisticCodeList = getPlatformCodeList(platform);
			visitor.setStatisticCodeList(statisticCodeList);
			visitorListByPlatform = visitorService
					.getRegisterListByChannel(visitor);
		}

		// pc,wap有两级渠道 app只有一级下载渠道
		Map<String, Integer> firstChennelMap = new HashMap<String, Integer>();

		// pc,wap
		String[] firstChannelArr = { "直接", "SEO", "SEM", "分享" };
		int[] firstAmountArr = new int[4];
		Map<String, Integer> SEOChennelMap = new HashMap<String, Integer>();
		Map<String, Integer> SEMChennelMap = new HashMap<String, Integer>();
		Map<String, Integer> shareChennelMap = new HashMap<String, Integer>();

		for (VisitorInfo visitorInfo : visitorListByPlatform) {
			int amount = visitorInfo.getAmount();
			String channelCode = visitorInfo.getChannelCode().replaceAll("\"","");
			if ("pc".equals(platform) || "2g53".equals(platform)
					|| "wap".equals(platform) || "2qw6".equals(platform)) {
				String channelName = statisticCodeService.getNameByCode(
						channelCode).getName();
				if (channelCode.equals("116yq5")) {
					firstAmountArr[0] += amount;
				} else if (channelCode.startsWith("123")) {
					firstAmountArr[1] += amount;
					SEOChennelMap.put(channelName, amount);
				} else if (channelCode.startsWith("12h")) {
					firstAmountArr[2] += amount;
					SEMChennelMap.put(channelName, amount);
				} else if (channelCode.startsWith("121")) {
					firstAmountArr[3] += amount;
					shareChennelMap.put(channelName, amount);
				}

			} else {
				firstChennelMap.put(channelCode, amount);
			}
		}
		// 获取前十条数据

		firstChennelMap = sortByValueDesc(firstChennelMap);
		int i = 0;
		int amount = 0;
		JSONArray arr = new JSONArray();
		for (Map.Entry<String, Integer> entry : firstChennelMap.entrySet()) {
			if (i < 10) {
				JSONObject o = new JSONObject();
				o.put("name",entry.getKey());
				o.put("value",entry.getValue());
				arr.add(o);
			} else {
				amount += entry.getValue();
			}
			i++;
		}
		if (firstChennelMap.size() > 10) {
			JSONObject o = new JSONObject();
			o.put("name","其他");
			o.put("value",amount);
			arr.add(o);
		}
		resultMap.put("jsonArr", arr);
		resultMap.put("firstChannelArr", firstChannelArr);
		resultMap.put("firstAmountArr", firstAmountArr);
		resultMap.put("SEOChennelMap", JSONObject.fromObject(SEOChennelMap)
				.toString());
		resultMap.put("SEMChennelMap", JSONObject.fromObject(SEMChennelMap)
				.toString());
		resultMap.put("shareChennelMap", JSONObject.fromObject(shareChennelMap)
				.toString());
		return resultMap;
	}

	/**
	 * @Title: sortByValueDesc
	 * @Description: map降序排序
	 * @param @param map
	 * @param @return 设定文件
	 * @return Map<K,V> 返回类型
	 * @throws
	 */
	private static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDesc(
			Map<K, V> map) {
		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
				map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
				return (o2.getValue()).compareTo(o1.getValue());
			}
		});
		Map<K, V> result = new LinkedHashMap<K, V>();
		for (Map.Entry<K, V> entry : list) {
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

	/**
	 * @Title: getChanenlData
	 * @Description: 获取最小粒度的渠道数据
	 * @param @param map
	 * @param @return 设定文件
	 * @return Map<String,Object> 返回类型
	 * @throws
	 */
	@RequestMapping("visitorChannelData")
	@ResponseBody
	public Map<String, Object> getChanenlData(String channelJson) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		JSONArray jsonArr = new JSONArray();
		int num = 0;
		if (StringUtils.isNotEmpty(channelJson)) {
			JSONObject jsonObject = JSONObject.fromObject(channelJson);
			for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
				String key = (String) iter.next();
				String value = jsonObject.getString(key);
				num += Integer.parseInt(value);
				JSONObject o = new JSONObject();
				o.put("name",key);
				o.put("value",Integer.parseInt(value));
				jsonArr.add(o);
			}
		}
		resultMap.put("num", num);
		resultMap.put("jsonArr", jsonArr);
		return resultMap;
	}

	/**
	 * @Title: orderAnalysis
	 * @Description: 订单分析
	 * @param @param model
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	@RequestMapping("orderAnalysis")
	public String orderAnalysis(ModelMap model) {
		return "application/orderAnalysis";
	}

	/**
	 * 订单图表数据
	 * 
	 * @param
	 * @param chooseTime
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("orderChartData")
	@ResponseBody
	public Map<String, Object> orderChartData(String chooseTime,
			String userUseYear, String flag) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		OrderInfo order = new OrderInfo();
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(chooseTime);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");
		order.setStartDate(beginDate);
		order.setEndDate(endDate);
		order.setUserUseYear(userUseYear);

		List<OrderInfo> lineChartlist = orderService.getListByDate(order);
		List<OrderInfo> columarChartlist = orderService
				.getListByCategory(order);
		// 柱状图数据
		List<String> categoryList = new ArrayList<>();
		JSONArray orderAmountArr2 = new JSONArray();
		JSONArray orderMoneyArr2 = new JSONArray();
		for (OrderInfo orderInfo : columarChartlist) {
			if(StringUtils.isNotEmpty(CommonParentType.getCategoryShowName(orderInfo
					.getCategoryId()))){
				categoryList.add(CommonParentType.getCategoryShowName(orderInfo
						.getCategoryId()));
				JSONObject orderAmountObj = new JSONObject();
				orderAmountObj.put("categoryName",CommonParentType.getCategoryShowName(orderInfo
						.getCategoryId()));
				JSONObject orderMoneyObj = new JSONObject();
				orderMoneyObj.put("categoryName",CommonParentType.getCategoryShowName(orderInfo
						.getCategoryId()));
				if ("order_num".equals(flag)) {
					orderAmountObj.put("amount",orderInfo.getOrderAmount());
				} else {
					orderMoneyObj.put("amount",new BigDecimal(orderInfo.getOrderMoney())
							.setScale(2, BigDecimal.ROUND_DOWN).doubleValue());
				}
				orderAmountArr2.add(orderAmountObj);
				orderMoneyArr2.add(orderMoneyObj);
			}
		}
		// 折线图数据
		String[] dateAxis = DateUtil.getBetweenTime(beginDate, endDate,
				"yyyy/MM/dd");
		List<String> dateList = Arrays.asList(dateAxis);
		JSONArray orderAmountArr = new JSONArray();
		JSONArray orderMoneyArr = new JSONArray();
		for(String date:dateList){
			for (String category : categoryList) {
				Integer orderAmountTomal = 0;
				Double orderMoneyTomal = 0.0;
				for (OrderInfo orderInfo : lineChartlist) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
					String time = sdf.format(orderInfo.getOperDate());
					if (category.equals(CommonParentType
							.getCategoryShowName(orderInfo.getCategoryId())) && date.equals(time)) {
						orderAmountTomal += orderInfo.getOrderAmount();
						orderMoneyTomal += new BigDecimal(
								orderInfo.getOrderMoney()).setScale(2,
								BigDecimal.ROUND_DOWN).doubleValue();
					}
				}
				JSONObject orderAmountObj = new JSONObject();
				orderAmountObj.put("categoryName",category);
				orderAmountObj.put("time",date);
				orderAmountObj.put("amount",orderAmountTomal);
				orderAmountArr.add(orderAmountObj);
				JSONObject orderMoneyObj = new JSONObject();
				orderMoneyObj.put("categoryName",category);
				orderMoneyObj.put("time",date);
				orderMoneyObj.put("amount",orderMoneyTomal);
				orderMoneyArr.add(orderMoneyObj);
			}
		}
		resultMap.put("categoryList", categoryList);
		resultMap.put("dateAxis", dateAxis);
		if ("order_num".equals(flag)) {
			resultMap.put("orderAmountJson", orderAmountArr);
			resultMap.put("orderAmountJson2", orderAmountArr2);
		} else {
			resultMap.put("orderMoneyJson2", orderMoneyArr2);
			resultMap.put("orderMoneyJson", orderMoneyArr);
		}
		return resultMap;
	}

	@RequestMapping("orderDataByCategory")
	@ResponseBody
	public Map<String, Object> orderDataByCategory(String chooseTime,
			String userUseYear, String flag, String categoryName) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		OrderInfo order = new OrderInfo();
		// 查询日期条件处理
		Map<String, Date> dateMap = getQueryDate(chooseTime);
		Date beginDate = dateMap.get("beginDate");
		Date endDate = dateMap.get("endDate");
		order.setStartDate(beginDate);
		order.setEndDate(endDate);
		order.setUserUseYear(userUseYear);
		order.setCategoryId(CommonParentType
				.getCategoryValueByShowName(categoryName));
		List<OrderInfo> orderlist = orderService.getListByPerform(order);

		if ("order_num".equals(flag)) {
			resultMap = getAmountMapByPlatform(orderlist);
		} else {
			resultMap = getMoneyMapByPlatform(orderlist);
		}
		return resultMap;
	}

	private Map<String, Object> getAmountMapByPlatform(List<OrderInfo> orderlist) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// pc,wap有两级渠道 app只有一级下载渠道
		Map<String, Integer> iosFirstChennelMap = new HashMap<String, Integer>();
		Map<String, Integer> androidFirstChennelMap = new HashMap<String, Integer>();
		// pc,wap
		String[] firstChannelArr = { "直接", "SEO", "SEM", "分享" };
		int[] firstAmountArr = new int[4];
		Map<String, Integer> SEOChennelMap = new HashMap<String, Integer>();
		Map<String, Integer> SEMChennelMap = new HashMap<String, Integer>();
		Map<String, Integer> shareChennelMap = new HashMap<String, Integer>();

		for (OrderInfo orderInfo : orderlist) {
			String orderJson = orderInfo.getOrderAmountJson();
			int orderAmount = 0;
			if (StringUtils.isNotBlank(orderJson)) {
				JSONObject jsonObject = JSONObject.fromObject(orderJson);
				for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
					String channelCode = (String) iter.next();
					int channelAmount = jsonObject.getInt(channelCode);
					orderAmount += channelAmount;
					if ("pc".equals(orderInfo.getPlatformCode())
							|| "wap".equals(orderInfo.getPlatformCode())) {
						String channelName = statisticCodeService
								.getNameByCode(channelCode).getName();
						if (channelCode.equals("116yq5")) {
							firstAmountArr[0] += orderAmount;
						} else if (channelCode.startsWith("123")) {
							firstAmountArr[1] += orderAmount;
							if (SEOChennelMap.containsKey(channelName)) {
								orderAmount += SEOChennelMap.get(channelName);
							}
							SEOChennelMap.put(channelName, orderAmount);
						} else if (channelCode.startsWith("12h")) {
							firstAmountArr[2] += orderAmount;
							if (SEMChennelMap.containsKey(channelName)) {
								orderAmount += SEMChennelMap.get(channelName);
							}
							SEMChennelMap.put(channelName, orderAmount);
						} else if (channelCode.startsWith("121")) {
							firstAmountArr[3] += orderAmount;
							if (shareChennelMap.containsKey(channelName)) {
								orderAmount += shareChennelMap.get(channelName);
							}
							shareChennelMap.put(channelName, orderAmount);
						}
					} else if ("ios".equals(orderInfo.getPlatformCode())) {
						if (iosFirstChennelMap.containsKey(channelCode)) {
							channelAmount += iosFirstChennelMap
									.get(channelCode);
						}
						iosFirstChennelMap.put(channelCode, channelAmount);
					} else {
						if (androidFirstChennelMap.containsKey(channelCode)) {
							channelAmount += androidFirstChennelMap
									.get(channelCode);
						}
						androidFirstChennelMap.put(channelCode, channelAmount);
					}
				}
			}
		}
		// 获取前十条数据
		iosFirstChennelMap = sortByValueDesc(iosFirstChennelMap);
		androidFirstChennelMap = sortByValueDesc(androidFirstChennelMap);
		int i = 0;
		int amount = 0;
		JSONArray iosJsonArr = new JSONArray();
		for (Map.Entry<String, Integer> entry : iosFirstChennelMap.entrySet()) {
			if (i < 10) {
				JSONObject o = new JSONObject();
				o.put("name",entry.getKey());
				o.put("value",entry.getValue());
				iosJsonArr.add(o);
			} else {
				amount += entry.getValue();
			}
			i++;
		}
		if (iosFirstChennelMap.size() > 10) {
			JSONObject o = new JSONObject();
			o.put("name","其他");
			o.put("value",amount);
			iosJsonArr.add(o);
		}
		i = 0;
		amount = 0;
		JSONArray androidJsonArr = new JSONArray();
		for (Map.Entry<String, Integer> entry : androidFirstChennelMap
				.entrySet()) {
			if (i < 10) {
				JSONObject o = new JSONObject();
				o.put("name",entry.getKey());
				o.put("value",entry.getValue());
				androidJsonArr.add(o);
			} else {
				amount += entry.getValue();
			}
			i++;
		}
		if (androidFirstChennelMap.size() > 10) {
			JSONObject o = new JSONObject();
			o.put("name","其他");
			o.put("value",amount);
			androidJsonArr.add(o);
		}

		resultMap.put("androidJsonArr", androidJsonArr);
		resultMap.put("iosJsonArr", iosJsonArr);
		resultMap.put("firstChannelArr", firstChannelArr);
		resultMap.put("firstAmountArr", firstAmountArr);
		resultMap.put("SEOChennelMap", JSONObject.fromObject(SEOChennelMap)
				.toString());
		resultMap.put("SEMChennelMap", JSONObject.fromObject(SEMChennelMap)
				.toString());
		resultMap.put("shareChennelMap", JSONObject.fromObject(shareChennelMap)
				.toString());
		return resultMap;
	}

	private Map<String, Object> getMoneyMapByPlatform(List<OrderInfo> orderlist) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// pc,wap有两级渠道 app只有一级下载渠道
		Map<String, Double> iosFirstChennelMap = new HashMap<String, Double>();
		Map<String, Double> androidFirstChennelMap = new HashMap<String, Double>();
		// pc,wap
		String[] firstChannelArr = { "直接", "SEO", "SEM", "分享" };
		double[] firstAmountArr = new double[4];
		Map<String, Double> SEOChennelMap = new HashMap<String, Double>();
		Map<String, Double> SEMChennelMap = new HashMap<String, Double>();
		Map<String, Double> shareChennelMap = new HashMap<String, Double>();

		for (OrderInfo orderInfo : orderlist) {
			String orderJson = orderInfo.getOrderMoneyJson();
			double orderAmount = 0;
			if (StringUtils.isNotBlank(orderJson)) {
				JSONObject jsonObject = JSONObject.fromObject(orderJson);
				for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();) {
					String channelCode = (String) iter.next();
					double channelAmount = jsonObject.getDouble(channelCode);
					orderAmount += channelAmount;
					if ("pc".equals(orderInfo.getPlatformCode())
							|| "wap".equals(orderInfo.getPlatformCode())) {
						String channelName = statisticCodeService
								.getNameByCode(channelCode).getName();
						if (channelCode.equals("116yq5")) {
							firstAmountArr[0] += orderAmount;
						} else if (channelCode.startsWith("123")) {
							firstAmountArr[1] += orderAmount;
							if (SEOChennelMap.containsKey(channelName)) {
								orderAmount += SEOChennelMap.get(channelName);
							}
							SEOChennelMap.put(
									channelName,
									new BigDecimal(orderAmount).setScale(2,
											BigDecimal.ROUND_DOWN)
											.doubleValue());
						} else if (channelCode.startsWith("12h")) {
							firstAmountArr[2] += orderAmount;
							if (SEMChennelMap.containsKey(channelName)) {
								orderAmount += SEMChennelMap.get(channelName);
							}
							SEMChennelMap.put(
									channelName,
									new BigDecimal(orderAmount).setScale(2,
											BigDecimal.ROUND_DOWN)
											.doubleValue());
						} else if (channelCode.startsWith("121")) {
							firstAmountArr[3] += orderAmount;
							if (shareChennelMap.containsKey(channelName)) {
								orderAmount += shareChennelMap.get(channelName);
							}
							shareChennelMap.put(
									channelName,
									new BigDecimal(orderAmount).setScale(2,
											BigDecimal.ROUND_DOWN)
											.doubleValue());
						}
					} else if ("ios".equals(orderInfo.getPlatformCode())) {
						if (iosFirstChennelMap.containsKey(channelCode)) {
							channelAmount += iosFirstChennelMap
									.get(channelCode);
						}
						iosFirstChennelMap.put(
								channelCode,
								new BigDecimal(channelAmount).setScale(2,
										BigDecimal.ROUND_DOWN).doubleValue());
					} else {
						if (androidFirstChennelMap.containsKey(channelCode)) {
							channelAmount += androidFirstChennelMap
									.get(channelCode);
						}
						androidFirstChennelMap.put(
								channelCode,
								new BigDecimal(channelAmount).setScale(2,
										BigDecimal.ROUND_DOWN).doubleValue());
					}
				}
			}
		}
		// 获取前十条数据
		iosFirstChennelMap = sortByValueDesc(iosFirstChennelMap);
		androidFirstChennelMap = sortByValueDesc(androidFirstChennelMap);
		JSONArray iosJsonArr = new JSONArray();
		JSONArray androidJsonArr = new JSONArray();
		int i = 0;
		double amount = 0;
		for (Map.Entry<String, Double> entry : iosFirstChennelMap.entrySet()) {
			if (i < 10) {
				JSONObject o = new JSONObject();
				o.put("name",entry.getKey());
				o.put("value",entry.getValue());
				iosJsonArr.add(o);
			} else {
				amount += entry.getValue();
			}
			i++;
		}
		if (iosFirstChennelMap.size() > 10) {
			JSONObject o = new JSONObject();
			o.put("name","其他");
			o.put("value",amount);
			iosJsonArr.add(o);
		}
		i = 0;
		amount = 0;
		for (Map.Entry<String, Double> entry : androidFirstChennelMap
				.entrySet()) {
			if (i < 10) {
				JSONObject o = new JSONObject();
				o.put("name",entry.getKey());
				o.put("value",entry.getValue());
				androidJsonArr.add(o);
			} else {
				amount += entry.getValue();
			}
			i++;
		}
		if (androidFirstChennelMap.size() > 10) {
			JSONObject o = new JSONObject();
			o.put("name","其他");
			o.put("value",amount);
			androidJsonArr.add(o);
		}

		resultMap.put("androidJsonArr", androidJsonArr);
		resultMap.put("iosJsonArr", iosJsonArr);

		resultMap.put("firstChannelArr", firstChannelArr);
		resultMap.put("firstAmountArr", firstAmountArr);
		resultMap.put("SEOChennelMap", JSONObject.fromObject(SEOChennelMap)
				.toString());
		resultMap.put("SEMChennelMap", JSONObject.fromObject(SEMChennelMap)
				.toString());
		resultMap.put("shareChennelMap", JSONObject.fromObject(shareChennelMap)
				.toString());
		return resultMap;
	}

	/**
	 * @Title: getQueryDate
	 * @Description: 查询条件起始时间的处理
	 * @param @param choose_date
	 * @param @return 设定文件
	 * @return Map<String,Date> 返回类型
	 * @throws
	 */
	private Map<String, Date> getQueryDate(String choose_date) {
		Map<String, Date> map = new HashMap<>();
		DateTimeFormatter formater = DateTimeFormat
				.forPattern("MM/dd/yyyy HH:mm:ss");
		Date beginDate = DateTime.parse(
				choose_date.split("-")[0].trim() + " 00:00:00", formater)
				.toDate();
		Date endDate = DateTime.parse(
				choose_date.split("-")[1].trim() + " 23:59:59", formater)
				.toDate();
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		return map;
	}

	/**
	 * @Title: getPlatformCodeList
	 * @Description: 获取平台相关code
	 * @param @param platform_code
	 * @param @return 设定文件
	 * @return List<String> 返回类型
	 * @throws
	 */
	private List<String> getPlatformCodeList(String platform_code) {
		List<String> statisticCodeList = statisticCodeService
				.getDeviceCodeByLikeName(platform_code);
		return statisticCodeList;
	}
}
